using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class AddtoCart : System.Web.UI.UserControl 
{
    processProduct pProduct;
    protected void Page_Load(object sender, EventArgs e)
    {
        //string ProductID = Request.QueryString["ProductID"];
        pProduct = new processProduct();
        //
        if (!Page.IsPostBack)
        {
            Load_gvwCart();
            
        }        
    }

    public void Load_gvwCart()
    {   
        DataTable dt = (DataTable)Session["Cart"];        
        if (dt == null)
        {
            lblResult.Text = "Has No product in your cart!";
            btnCheckOut.Visible = false;
            btnUpdate.Visible = false;
            lblTotalMerchandise.Visible = false;
            Label1.Visible = false;
            Label2.Visible = false;
        }        
        else 
        {
            gvwCart.DataSource = dt.DefaultView;
            gvwCart.DataBind();
        }        
        
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        double TotalMerchandise=0;
        DataTable dtCart = (DataTable)Session["Cart"];      
        for (int i = gvwCart.Rows.Count-1; i >= 0; i--)
        {
            CheckBox cb = (CheckBox)gvwCart.Rows[i].FindControl("chkRemove");
            if (cb.Checked)
            {
                dtCart.Rows.RemoveAt(i);
            }
            else
            {
                TextBox txtProductQuantity = (TextBox)gvwCart.Rows[i].FindControl("txtProductQuantity");
                int value = Int32.Parse(txtProductQuantity.Text);
                dtCart.Rows[i]["Quantity"] = value;
                double unitPrice = Double.Parse(dtCart.Rows[0]["ProductPrice"].ToString());
                double total = unitPrice * value;
                dtCart.Rows[i]["Total"] = total;
                TotalMerchandise += total;
            }
        }        
        lblTotalMerchandise.Text = TotalMerchandise.ToString();      
        Session["Cart"] = dtCart;
        //load lai nhung ko visiable button
        Load_gvwCart();        
    }
    protected void btnContinue_Click(object sender, EventArgs e)
    {
        Response.Redirect("index.aspx");
    }
    protected void btnCheckOut_Click(object sender, EventArgs e)
    {
        string ProductID = Request.QueryString["ProductID"];
        Response.Redirect("index.aspx?subpara=checkout&ProductID=" + ProductID );
    }
    
    protected void gvwCart_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            TextBox txtQuantity = (TextBox)e.Row.FindControl("txtProductQuantity");
            txtQuantity.Text= ((DataRowView)e.Row.DataItem)["Quantity"].ToString();
            Label lblTotal = (Label)e.Row.FindControl("lblTotal");
            lblTotal.Text = ((DataRowView)e.Row.DataItem)["Total"].ToString();            
        }
    }
    protected void gvwCart_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvwCart.PageIndex = e.NewPageIndex;
        Load_gvwCart();
    }
}
